package com.glepoch.springbootsecuritymybatis.utils;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;


/**
 * 二维码工具类
 *
 * @author limingcheng
 */
@Component
public class QrCodeUtil {


    /**
     * 生成一个二维码图片
     *
     * @param width
     * @param height
     * @param content
     * @return
     * @throws WriterException
     * @throws IOException
     */

    public static byte[] createQRCode(int width, int height, String content) throws WriterException, IOException {

// 二维码基本参数设置

        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();

        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码字符集utf-8

        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 设置纠错等级L/M/Q/H,纠错等级越高越不易识别，当前设置等级为最高等级H

        hints.put(EncodeHintType.MARGIN, 0);// 可设置范围为0-10，但仅四个变化0 1(2) 3(4 5 6) 7(8 9 10)

// 生成图片类型为QRCode

        BarcodeFormat format = BarcodeFormat.QR_CODE;

// 创建位矩阵对象

        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, format, width, height, hints);

// 设置位矩阵转图片的参数

//        MatrixToImageConfig config = new MatrixToImageConfig(Color.black.getRGB(), Color.white.getRGB());

// 位矩阵对象转流对象

        ByteArrayOutputStream os = new ByteArrayOutputStream();

        MatrixToImageWriter.writeToStream(bitMatrix, "png", os);

        return os.toByteArray();

    }

    /*读取二维码*/
    public static String readQR() {
        try {
            MultiFormatReader formatReader = new MultiFormatReader();

            File file = new File("E:\\bestme.png");

            BufferedImage image = ImageIO.read(file);

            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));

            // 定义二维码的参数
            HashMap map = new HashMap();
            map.put(EncodeHintType.CHARACTER_SET, "utf-8");// 编码

            Result result = formatReader.decode(bitmap, map);

            System.out.println("解析结果: " + result.toString());
            System.out.println("二维码格式类型: " + result.getBarcodeFormat());
            System.out.println("二维码文本内容: " + result.getText());
            return result.toString();
        } catch (NotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) throws WriterException, IOException {

        byte[] b = createQRCode(100, 100, "http://qbu6ixiy.ipyingshe.net");

        OutputStream os = new FileOutputStream("E:\\bestme.png");

        os.write(b);

        os.close();
        readQR();

    }

}